<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="logoBox">
        <img src="@/assets/heima.png" alt="">
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出登录</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="collapseButton" @click="collapseAside">
          <span>{{isCollapse ? '展开' : '收起'}}</span>
        </div>
        <el-menu :collapse="isCollapse" :collapse-transition="false" unique-opened background-color="#333744" text-color="#fff" active-text-color="#ffd04b" router :default-active="thisIndex">
          <!-- 菜单一 -->
          <el-submenu v-for="item in menuList" :key="item.id" :index="item.order+''">
            <!-- 一级菜单 -->
            <template slot="title">
              <i :class="iconList[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-for="child in item.children" :key="child.id" :index="'/'+child.path">
              <i class="el-icon-menu"></i>
              <span>{{child.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import $http from '@/utils/resquest.js'
export default {
  data() {
    return {
      // 菜单栏数据
      menuList: [],
      // 字体图标
      iconList: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-tools',
        101: 'el-icon-s-goods',
        102: 'el-icon-document',
        145: 'el-icon-s-data'
      },
      // 控制折叠
      isCollapse: false,
      // 当前访问路由index
      thisIndex: ''
    }
  },
  methods: {
    logout() {
      // 清除token
      sessionStorage.clear()
      // 跳转到登录页
      this.$router.push('/login')
    },
    // 获取左侧菜单列表
    async getMenuList() {
      const { data: res } = await $http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 点击收起菜单栏
    collapseAside() {
      this.isCollapse = !this.isCollapse
    },
    // 保持选中栏高亮
    defaultActive() {
      this.thisIndex = this.$route.path
    }
  },
  created() {
    this.getMenuList()
    this.defaultActive()
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    .logoBox {
      display: flex;
      align-items: center;
      span {
        color: #fff;
        margin-left: 20px;
        font-size: 20px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .el-menu {
      border-right: 0;
    }
    .collapseButton {
      height: 30px;
      line-height: 30px;
      font-size: 14px;
      color: #fff;
      text-align: center;
      background-color: #4a5064;
      cursor: pointer;
      letter-spacing: 0.2em;
    }
  }
  .el-main {
    background-color: #eaedfa;
  }
}
</style>